package com.cloudeasy.user.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.EntityUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.user.UserHideInfoVO;
import com.cloudeasy.api.vo.user.UserInfoVO;
import com.cloudeasy.user.field.UserInfoField;
import com.cloudeasy.user.po.UserInfo;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息工具类
 */
public class UserInfoUtil {
    /**
     * 构造查询
     *
     * @param userIds   用户Id列表
     * @param nicknames 昵称列表
     * @param asc       正序排序字段
     * @param desc      倒序排序字段
     * @return {@link QueryWrapper<UserInfo>}
     */
    public static QueryWrapper<UserInfo> compose(List<String> userIds, List<String> nicknames, String[] asc, String[] desc) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if (userIds != null && userIds.size() > 0) {
            queryWrapper.in("id", userIds);
        }
        if (nicknames != null && nicknames.size() > 0) {
            queryWrapper.and(userInfoQueryWrapper -> {
                nicknames.forEach(nickname -> userInfoQueryWrapper.or(userInfoTempQueryWrapper ->
                        userInfoTempQueryWrapper.like("nickname", nickname)));
                return userInfoQueryWrapper;
            });
        }
        if (asc != null && asc.length > 0) {
            EntityUtil.checkParamter(UserInfoField.getFieldList(), asc);
            queryWrapper.orderByAsc(asc);
        }
        if (desc != null && desc.length > 0) {
            EntityUtil.checkParamter(UserInfoField.getFieldList(), desc);
            queryWrapper.orderByDesc(desc);
        }
        return queryWrapper;
    }

    /**
     * 转换VO对象
     *
     * @param userInfo {@link UserInfo}
     * @return {@link UserInfoVO}
     */
    public static UserInfoVO convert(UserInfo userInfo) {
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo, userInfoVO);
        // 如果信息公开
        if (userInfo.getExpose()) {
            UserHideInfoVO userHideInfoVO = new UserHideInfoVO();
            BeanUtils.copyProperties(userInfo, userHideInfoVO);
            userInfoVO.setUserHideInfo(userHideInfoVO);
        }
        return userInfoVO;
    }

    /**
     * 转换VO对象
     *
     * @param userInfos {@link List<UserInfo>}
     * @return {@link List<UserInfoVO>}
     */
    public static List<UserInfoVO> convert(List<UserInfo> userInfos) {
        return userInfos.stream().map(UserInfoUtil::convert).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param userInfoIPage {@link IPage<UserInfo>}
     * @return {@link PageResult<UserInfoVO>}
     */
    public static PageResult<UserInfoVO> convert(IPage<UserInfo> userInfoIPage) {
        PageResult<UserInfoVO> pageResult = new PageResult<>();
        pageResult.setTotal(userInfoIPage.getTotal())
                .setPages(userInfoIPage.getPages())
                .setRecords(userInfoIPage.convert(UserInfoUtil::convert).getRecords());
        return pageResult;
    }
}
